//
//  WorkHomeVC.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//

#import "NotesGuideAlertView.h"
#import "MJTitleButtonView.h"
@interface NotesGuideAlertView ()

@property (weak, nonatomic) IBOutlet MJUIAlertVCBaseView *baseAlertView;
@property (weak, nonatomic) IBOutlet UIView *contentview;
@property (weak, nonatomic) IBOutlet UIButton *flagButton;
@property (weak, nonatomic) IBOutlet UIView *flagStackBgView;
@property (weak, nonatomic) IBOutlet UILabel *messageLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentviewH;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconImageViewH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconImageViewW;

//@property (weak, nonatomic) IBOutlet UIButton *cancelButton;
//@property (weak, nonatomic) IBOutlet UIButton *confirmButton;
@property (weak, nonatomic) IBOutlet UIView *lineView;

@property (nonatomic,strong) NSTimer *countdownTimer;
@property (nonatomic,assign) NSInteger secondTime;

//
@property (weak, nonatomic) IBOutlet MJTitleButtonView *cancelView;
@property (weak, nonatomic) IBOutlet MJTitleButtonView *confirmView;

@end

@implementation NotesGuideAlertView
#pragma mark - 系统



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.baseAlertView initContentview:self.contentview];
    
    
    [self.flagButton setImage:MJUIImageMake(@"icon_chose1") forState:UIControlStateNormal];
    [self.flagButton setImage:MJUIImageMake(@"icon_chose2") forState:UIControlStateSelected];
    [self.flagButton mj_addAction:^(UIButton *button) {
        [button setSelected:!button.isSelected];
    }];
    self.messageLabel.lineBreakMode = NSLineBreakByWordWrapping; // 设置按单词换行

    CGFloat imageViewH = 74;
    if(self.alertViewType == CenterAlertViewTypeGuide){
        imageViewH = 74;
        self.iconImageView.image = MJUIImageMake(@"bg_bofang");
        self.iconImageViewH.constant = imageViewH;
        self.iconImageViewW.constant = 126;
        self.titleLabel.mj_localizable = @"Important Notes";
        self.messageLabel.mj_localizable = @"Before proceeding, please watch the important notes for the correct operational procedure.";
    }
    if(self.alertViewType == CenterAlertViewTypeUploadFailed){
        imageViewH = 129;
        self.iconImageView.image = MJUIImageMake(@"bg_shangchuanshibai");
        self.iconImageViewH.constant = imageViewH;
        self.iconImageViewW.constant = 177;
        self.titleLabel.mj_localizable = @"Upload Failed";
        self.messageLabel.mj_localizable = @"Server connection lost. Please check network and try again.";
        self.cancelView.hidden = true;
        [self.cancelView updateTitleSpace];
        [self.confirmView updateTitleSpace];
        self.confirmView.titleLabel.mj_localizable = @"Try Again";
        self.lineView.hidden = true;
        self.flagStackBgView.hidden = true;
    }
    if(self.alertViewType == CenterAlertViewTypeNewDevice){
        imageViewH = 107;
        self.iconImageView.image = MJUIImageMake(@"bg_liangjie");
        self.iconImageViewH.constant = imageViewH;
        self.iconImageViewW.constant = 190;
        self.titleLabel.mj_localizable = @"New Meter Detected";
        self.messageLabel.mj_localizable = @"A new Meter has been detected.\nWould you like to connect?";
        self.confirmView.titleLabel.mj_localizable = @"Connect";
        self.cancelView.titleLabel.mj_localizable = @"Search Again";
        self.flagStackBgView.hidden = true;
    }
    if(self.alertViewType == CenterAlertViewTypeTestStrip){
        imageViewH = 143;
        self.iconImageView.image = MJUIImageMake(@"bg_meishizhi");
        self.iconImageViewH.constant = imageViewH;
        self.iconImageViewW.constant = 108;
        self.titleLabel.mj_localizable = @"Test Strip Inconsistent";
        self.messageLabel.mj_localizable = @"Test strip mismatch. Switch tests or replace strip?";
        self.confirmView.titleLabel.mj_localizable = @"Replace Strip";
        self.cancelView.titleLabel.mj_localizable = @"Switch Tests";
        self.flagStackBgView.hidden = true;
    }
    if(self.alertViewType == CenterAlertViewTypeMatchCode){
        imageViewH = 171;
        self.iconImageView.image = MJUIImageMake(@"bg_code");
        self.iconImageViewH.constant = imageViewH;
        self.iconImageViewW.constant = 254;
        self.titleLabel.mj_localizable = @"Match Code Number";
        self.messageLabel.mj_localizable = @"Press C button on meter to match the code number on the test strip bottle before sampling.";
        self.confirmView.titleLabel.mj_localizable = @"Completed";
        self.cancelView.hidden = true;
        [self.cancelView updateTitleSpace];
        [self.confirmView updateTitleSpace];
        self.lineView.hidden = true;
        self.confirmView.titleLabel.textColor = UIColor.whiteColor;
        self.confirmView.titleLabel.backgroundColor = UIColor.mj_appColor;
        self.secondTime = 5;
        [self.countdownTimer setFireDate:[NSDate distantPast]]; //开启
        self.confirmView.doneButton.mj_enabled = false;
        
    }
    if(self.alertViewType == CenterAlertViewTypeRetestProject){
        imageViewH = 105;
        self.iconImageView.image = MJUIImageMake(@"icon_tihuan_d");
        self.iconImageViewH.constant = imageViewH;
        self.iconImageViewW.constant = 240;
        self.titleLabel.mj_localizable = @"Retest Project";
        NSString *message = [NSString stringWithFormat:@"%@ %@",MJUILocalizedString(self.detectionItemName),MJUILocalizedString(@"has been tested. D you like to retest and replace the result?")];
        self.messageLabel.mj_localizable = message;
        self.confirmView.titleLabel.mj_localizable = @"Retest";
        self.cancelView.titleLabel.mj_localizable = @"Replace Strip";
        self.flagStackBgView.hidden = true;
    }
    
    CGSize size222 = [self.messageLabel sizeThatFits:CGSizeMake(SCREEN_WIDTH - 120, MAXFLOAT)];
    
    self.contentviewH.constant = size222.height + imageViewH + 170 + (self.flagStackBgView.hidden ? 0 : 35);
    
    //
    MJWeakSelf
    [self.cancelView.doneButton mj_addAction:^(UIButton *button) {
        [weakSelf handleCancelButtonButtonEvent:button];
    }];
    [self.confirmView.doneButton mj_addAction:^(UIButton *button) {
        [weakSelf handleConfirmButtonButtonEvent:button];
    }];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
}

- (IBAction)handleFlaguttonButtonEvent:(UIButton *)sender {
    [self.flagButton setSelected:!self.flagButton.isSelected];

}
- (void)handleConfirmButtonButtonEvent:(UIButton *)sender {

    if(self.flagButton.isSelected && self.alertViewType == CenterAlertViewTypeGuide){
        [MJUIUserDeFault setObject:@"1" forKey:NotesGuide_Open];
    }
    if(self.flagButton.isSelected && self.alertViewType == CenterAlertViewTypeMatchCode){
        [MJUIUserDeFault setObject:@"1" forKey:MatchCodeNumber_Open];
    }
    MJWeakSelf
    [self dismissViewControllerAnimated:false completion:^{
        if(weakSelf.confirmBlock){
            weakSelf.confirmBlock();
        }
    }];
}

- (void)handleCancelButtonButtonEvent:(UIButton *)sender {
    
    if(self.flagButton.isSelected && self.alertViewType == CenterAlertViewTypeGuide){
        [MJUIUserDeFault setObject:@"1" forKey:NotesGuide_Open];
    }
    MJWeakSelf
    [self dismissViewControllerAnimated:false completion:^{
        if(weakSelf.cancelBlock){
            weakSelf.cancelBlock();
        }
    }];
}

#pragma mark - 其他
- (void)updateTime{

    if(self.secondTime > 0){
        self.confirmView.titleLabel.text = [NSString stringWithFormat:@"%ldS",self.secondTime];
//        [self.confirmButton setTitle:[NSString stringWithFormat:@"%ldS",self.secondTime] forState:0];
    }else{
        self.confirmView.titleLabel.mj_localizable = @"Completed";
        [self.countdownTimer invalidate];
        self.countdownTimer = nil;
        self.confirmView.doneButton.mj_enabled = true;
    }
    self.secondTime--;
}
#pragma mark - 代理

#pragma mark - 懒加载
- (NSTimer *)countdownTimer{
    if (!_countdownTimer) {
        _countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                                            target:self
                                                          selector:@selector(updateTime)
                                                          userInfo:nil
                                                           repeats:YES];
        
        [[NSRunLoop mainRunLoop] addTimer:_countdownTimer forMode:NSRunLoopCommonModes];

    }
    return _countdownTimer;
}
@end
